//
//  LSPersonalHomePageVController.m
//  LookScore
//
//  Created by wr on 2017/12/18.
//  Copyright © 2017年 ChongQingXunTiCompany. All rights reserved.
//

#import "LSPersonalHomePageVController.h"
#import "LSHttpEnclosureTool.h"
#import <MJRefresh.h>
#import "LSPersonalHomePageHeadView.h"
#import "LSPersonalHomePageCharmCell.h"
#import "LSChatCircleCell.h"//聊天圈和发布话题一样cell
#import "LSChatCircleModel.h"
#import "LSPersonalHomePageCharmModel.h"
#import <AVFoundation/AVCaptureDevice.h>
#import <AVFoundation/AVMediaFormat.h>
#import <AVFoundation/AVFoundation.h>
#import <Photos/PHPhotoLibrary.h>
#import <AssetsLibrary/AssetsLibrary.h>
#import "LSAccountArchive.h"
#import "LSEditIntroduceViewController.h"
#import "WRNavigationBar.h"
#import "ConversationDetailController.h"
#import "LSOtherHomePageViewController.h"

@interface LSPersonalHomePageVController ()<UITableViewDelegate,UITableViewDataSource,LSPersonalHomePageCharmCellDelagate,LSPersonalHomePageScreenCellDelagate>
{
    LSPersonalHomePageHeadView *headView;
    NSString * _tokenStr;
}

@property(strong,nonatomic)UITableView *tableView;

@property(strong,nonatomic)NSMutableArray *modelArray;

@property(assign,nonatomic)NSInteger  chooseTag;

@property(assign,nonatomic)NSInteger  pageIndex;

@property(assign,nonatomic)NSInteger  queryType;//1关注 2粉丝
@end

@implementation LSPersonalHomePageVController

-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor =LSBackGroundColor;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tableView;
}


- (NSMutableArray *)modelArray{
    if(!_modelArray){
        _modelArray = @[].mutableCopy;
    }
    return _modelArray;
}


- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.translucent=YES;
}
//-(void)viewDidDisappear:(BOOL)animated{
//    [super viewDidDisappear:YES];
//    self.navigationController.navigationBar.hidden=NO;
//}
- (void)viewDidLoad {
    [super viewDidLoad];
    _tokenStr = [LSAccountArchive isLogin] ? [LSAccountArchive account].Cid : @"";
    [self wr_setNavBarBackgroundAlpha:0];
    self.chooseTag = 0;
    [notifiCenter addObserver:self selector:@selector(updateUserInfo) name:@"LOADCIRCLEDATA" object:nil];
    
    [self loadTopViewData];
    LSRefreshHeaderView *header = [LSRefreshHeaderView headerWithRefreshingBlock:^{
        [self loadTableViewData:YES];
        
    }];
    self.tableView.mj_header = header;// 设置header
    [header beginRefreshing];
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        [self loadTableViewData:NO];
    }];
    
    
    [self initTopUI];
    
}

- (void)updateUserInfo{
    [self loadTopViewData];
    [self loadTableViewData:YES];
}
-(void)loadTopViewData{
//    headView.bottomArray = nil;
//    [headView.bottomArray removeAllObjects];
    NSDictionary *userIndexInfoDic=@{@"cid":self.userCid,@"loginCid":[LSAccountArchive account].Cid};
    [LSHttpEnclosureTool GET:LSUserIndexInfo parameters:userIndexInfoDic resBlock:^(id responseObject) {
        if (responseObject) {
            MyLog(@"topViewData = %@",responseObject);
            [self setData:responseObject];
        }
    }];
}

- (void)loadTableViewData:(BOOL)refresh{
    if (refresh) {
        
        self.pageIndex = 0;
    }else{
        
        self.pageIndex += 1;
    }
    
    MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:[UIApplication sharedApplication].keyWindow animated:YES];
    //    hud.label.text=@"正在请求中...";
    hud.mode=MBProgressHUDModeIndeterminate;
//    self.pageIndex = 0;
    if (self.chooseTag == 0) {
        [LSHttpEnclosureTool GET:LSPublishList parameters:@{@"cid":self.userCid,@"loginCid":[LSAccountArchive account].Cid,@"pageIndex":@(self.pageIndex)} resBlock:^(id responseObject) {
            if (refresh) {
                [self.modelArray removeAllObjects];
            }
            if (responseObject) {
                for (NSDictionary * dict in responseObject[@"TopicList"]) {
                    LSChatCircleModel *model = [LSChatCircleModel mj_objectWithKeyValues:dict];
                    
                    [self.modelArray addObject:model];
                }
            }else{
                
                self.pageIndex -= 1;
                
            }
            [hud hideAnimated:YES];
            MyLog(@"tableViewData:%@",responseObject);
            [self.tableView reloadData];
            [CommonClass endRefreshing:self.tableView];
        }];
        
    }else if (self.chooseTag == 1){
        
        [LSHttpEnclosureTool GET:LSJoinList parameters:@{@"cid":self.userCid,@"loginCid":[LSAccountArchive account].Cid,@"pageIndex":@(self.pageIndex)} resBlock:^(id responseObject) {
            if (refresh) {
                [self.modelArray removeAllObjects];
            }
            if (responseObject) {
                for (NSDictionary * dict in responseObject[@"JoinList"]) {
                    MyLog(@"LSJoinList --- %@",responseObject);
                    LSChatCircleModel *model = [LSChatCircleModel mj_objectWithKeyValues:dict];
                    [self.modelArray addObject:model];
                }
            }else{
                
                self.pageIndex -= 1;
                
            }
            [hud hideAnimated:YES];
//            MyLog(@"tableViewData:%@",responseObject);
            [self.tableView reloadData];
            [CommonClass endRefreshing:self.tableView];
        }];
        
    }else if (self.chooseTag == 2){
        self.queryType = 1;
        
        [LSHttpEnclosureTool GET:LSUSerFans parameters:@{@"cid":self.userCid,@"loginCid":[LSAccountArchive account].Cid,@"pageIndex":@(self.pageIndex),@"type":@(self.queryType)} resBlock:^(id responseObject) {
            if (refresh) {
                [self.modelArray removeAllObjects];
            }
            MyLog(@"tableViewData:%@",responseObject);
            if (responseObject) {
                for (NSDictionary * dict in responseObject[@"UserList"]) {
                    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:dict];
                    [dic setObject:@(self.queryType) forKey:@"queryType"];
                    LSPersonalHomePageCharmModel *model = [LSPersonalHomePageCharmModel mj_objectWithKeyValues:dict];

                    [self.modelArray addObject:model];
//                    MyLog(@"tableViewData:%@",dic);
                }
                
            }else{
                
                self.pageIndex -= 1;
                
            }
            [hud hideAnimated:YES];
            [self.tableView reloadData];
            [CommonClass endRefreshing:self.tableView];
        }];
        
    }else if (self.chooseTag == 3){
        self.queryType = 2;
        
        [LSHttpEnclosureTool GET:LSUSerFans parameters:@{@"cid":self.userCid,@"loginCid":[LSAccountArchive account].Cid,@"pageIndex":@(self.pageIndex),@"type":@(self.queryType)} resBlock:^(id responseObject) {
            if (refresh) {
                [self.modelArray removeAllObjects];
            }
            if (responseObject) {
                for (NSDictionary * dict in responseObject[@"UserList"]) {
                     NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:dict];
                    [dic setObject:@(self.queryType) forKey:@"queryType"];
                    
                    LSPersonalHomePageCharmModel *model = [LSPersonalHomePageCharmModel mj_objectWithKeyValues:dic];
                    
                    [self.modelArray addObject:model];
                }
                MyLog(@"tableViewData:%@",responseObject);
            }
            else{
                
                self.pageIndex -= 1;
                
            }
            [hud hideAnimated:YES];
            [self.tableView reloadData];
            [CommonClass endRefreshing:self.tableView];
        }];
    }else{
        [LSHttpEnclosureTool GET:LSScreenUserList parameters:@{@"cid":[LSAccountArchive account].Cid,@"pageIndex":@(self.pageIndex)} resBlock:^(id responseObject) {
            if (refresh) {
                [self.modelArray removeAllObjects];
            }
            if (responseObject) {
                for (NSDictionary * dict in responseObject[@"UserList"]) {
                    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:dict];
//                    [dic setObject:@(self.queryType) forKey:@"queryType"];

                    LSPersonalHomePageCharmModel *model = [LSPersonalHomePageCharmModel mj_objectWithKeyValues:dic];

                    [self.modelArray addObject:model];
                }
                MyLog(@"tableViewData:%@",responseObject);
            }
            else{
                
                self.pageIndex -= 1;
                
            }
            [hud hideAnimated:YES];
            [self.tableView reloadData];
            [CommonClass endRefreshing:self.tableView];
        }];
    }
    
    
    
}

//- (void)loadMoreTableViewData{
//    self.pageIndex += 1;
//    [self loadTableViewData];
//}

//给topUI赋值
- (void)setData:(NSDictionary *)userindexInfo{
    
    NSArray * array =  @[[NSString stringWithFormat:@"发布的话题\n(%@)",userindexInfo[@"TopicPublishCount"]],[NSString stringWithFormat:@"参与的话题\n(%@)",userindexInfo[@"TopicJoinCount"]],[NSString stringWithFormat:@"关注\n(%@)",userindexInfo[@"AttentionsCount"]],[NSString stringWithFormat:@"粉丝\n(%@)",userindexInfo[@"FansCount"]],[NSString stringWithFormat:@"黑名单\n(%@)",userindexInfo[@"ScreenUserCount"]]];
    [headView addFasleData];
    headView.sortView.dataArray = array.mutableCopy;
//    headView.dict = userindexInfo;
//    [headView.bottomArray addObjectsFromArray:@[[NSString stringWithFormat:@"发布的话题\n(%@)",userindexInfo[@"TopicPublishCount"]],[NSString stringWithFormat:@"参与的话题\n(%@)",userindexInfo[@"TopicJoinCount"]],[NSString stringWithFormat:@"关注\n(%@)",userindexInfo[@"AttentionsCount"]],[NSString stringWithFormat:@"粉丝\n(%@)",userindexInfo[@"FansCount"]]]];
}

#pragma mark 🔥 tableViewDelegate tableViewDataSource

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.modelArray.count;

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    if (self.chooseTag==0) {
        id  model = self.modelArray[indexPath.row];
        return [self.tableView cellHeightForIndexPath:indexPath model:model keyPath:@"model" cellClass:[LSChatCircleCell class] contentViewWidth:[tableView cellContentViewWith]];
    }else if (self.chooseTag == 1){
        
        id model = self.modelArray[indexPath.row];
        return [self.tableView cellHeightForIndexPath:indexPath model:model keyPath:@"model" cellClass:[LSChatCirclePartakeCell class] contentViewWidth:[tableView cellContentViewWith]];
    }
     return 70;
}


- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (self.chooseTag==0) {
        
        LSChatCircleCell *cell=[LSChatCircleCell ChatCircleTableViewCellByTableView:tableView];
        LSChatCircleModel * model = self.modelArray[indexPath.row];
        cell.reportBu.hidden = YES;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.isShow = YES;
        cell.model = model;
//        cell.clickedImageViewBlock = ^{
//            if ([LSAccountArchive isLogin]) {
//                LSOtherHomePageViewController * otherVC = [[LSOtherHomePageViewController alloc] init];
//                otherVC.userCid = model.UserCid;
//                [self.navigationController pushViewController:otherVC animated:YES];
//            }else{
//                [self presentLoginVC];
//            }
//
//        };
        return cell;
        
    }else if (self.chooseTag==1){
        
        LSChatCirclePartakeCell * cell = [LSChatCirclePartakeCell ChatCirclePartakeTableViewCellByTableView:tableView];
        LSChatCircleModel * model = self.modelArray[indexPath.row];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.model = model;
        cell.attentionBu.hidden = YES;
//        cell.clickedImageViewBlock = ^{
//            if ([LSAccountArchive isLogin]) {
//                LSOtherHomePageViewController * otherVC = [[LSOtherHomePageViewController alloc] init];
//                otherVC.userCid = model.UserCid;
//                [self.navigationController pushViewController:otherVC animated:YES];
//            }else{
//                [self presentLoginVC];
//            }
//            
//        };
        return cell;
        
    }
    else if (self.chooseTag == 4){
        LSPersonalHomePageScreenCell *cell=[LSPersonalHomePageScreenCell PersonalHomePageScreenCellByTableView:tableView];
        LSPersonalHomePageCharmModel *model = self.modelArray[indexPath.row];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.model = model;
        //        cell.cellType = self.chooseTag;
        cell.delegate = self;
        return cell;
     
    }else{
        LSPersonalHomePageCharmCell *cell=[LSPersonalHomePageCharmCell PersonalHomePageCharmCellByTableView:tableView];
        LSPersonalHomePageCharmModel *model = self.modelArray[indexPath.row];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.model = model;
//        cell.cellType = self.chooseTag;
        cell.delegate = self;
        return cell;
    }
    
    
    return nil;
}


- (CGFloat )tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (self.chooseTag < 2) {
        return 10;
    }else{
        
        return self.modelArray.count;
    }
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    
    return 0.01;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    
    return nil;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    return nil;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.chooseTag < 2) {
        LSChatCircleModel * model = self.modelArray[indexPath.row];
        ConversationDetailController *detailVC=[[ConversationDetailController alloc]init];
        if (self.chooseTag == 0) {
            detailVC.TopicId = model.Id;
        }else if (self.chooseTag == 1){
            detailVC.TopicId = model.TopicId;
        }
        detailVC.cid = _tokenStr;
        [self.navigationController pushViewController:detailVC animated:YES];
    }else if (self.chooseTag >= 2 && self.chooseTag < 4){
        LSOtherHomePageViewController *OHVC = [[LSOtherHomePageViewController alloc] init];
        LSPersonalHomePageCharmModel *model = self.modelArray[indexPath.row];
        if (!model.IsScreened){
            
            OHVC.userCid = model.Cid;
            OHVC.OtherAttentionBlock = ^{
                [self loadTopViewData];
                [self loadTableViewData:YES];
                if (self.PersonalDataChangedBlock) {
                    self.PersonalDataChangedBlock();
                }
            };
            [self.navigationController pushViewController:OHVC animated:YES];
        }else {
            [MBProgressHUD showOnlyMessage:@"您已拉黑该好友，无法查看他的主页！" afterDelay:1.5f];
        }
        
    }else {
        [MBProgressHUD showOnlyMessage:@"您已拉黑该好友，无法查看他的主页！" afterDelay:1.5f];
    }
}

#pragma mark 🔥 LSPersonalHomePageScreenCellDelagate
- (void)screenEvent:(UIButton *)sender CidString:(NSString *)string{
    
    sender.enabled = NO;
    [LSHttpEnclosureTool POST:LSAddUserScreenUser parameters:@{@"usercid":string,@"loginUsercid":[LSAccountArchive account].Cid} resBlock:^(id responseObject) {
        if (responseObject) {
            MyLog(@"responseObject --- %@",responseObject);
            [self loadTopViewData];
            [self loadTableViewData:YES];
            sender.enabled = YES;
            [notifiCenter postNotificationName:@"LOADCIRCLEDATA" object:nil];
            if (self.PersonalDataChangedBlock) {
                self.PersonalDataChangedBlock();
            }
        }
        sender.enabled = YES;
    }];
}
#pragma mark 🔥 LSPersonalHomePageCharmCellDelagate
- (void)attentionEvent:(UIButton *)sender CidString:(NSString *)string{
    sender.enabled = NO;
    [LSHttpEnclosureTool POST:LSUserAttention parameters:@{@"fanCid":[LSAccountArchive account].Cid,@"Cid":string} resBlock:^(id responseObject) {
        if (responseObject) {
            MyLog(@"responseObject --- %@",responseObject);
            [self loadTopViewData];
            [self loadTableViewData:YES];
            sender.enabled = YES;
            [notifiCenter postNotificationName:@"LOADCIRCLEDATA" object:nil];
            if (self.PersonalDataChangedBlock) {
                self.PersonalDataChangedBlock();
            }
        }
        sender.enabled = YES;
    }];
}
#pragma mark 🔥 事件
- (void)presentLoginVC{
    
    LSLoginVController *loginVC=[[LSLoginVController alloc]init];
    [self presentRootViewController:loginVC animated:YES completion:nil];
    loginVC.loginSuccessBlock = ^{
        [self.tableView reloadData];
        _tokenStr = [LSAccountArchive account].Cid;
    };
    
}


-(void)segmentSortButtonEvent:(NSInteger)tag{
    NSIndexPath* indexPat = [NSIndexPath indexPathForRow:0 inSection:0];
    [self.tableView scrollToRowAtIndexPath:indexPat atScrollPosition:UITableViewScrollPositionBottom animated:YES];
    switch (tag) {
        case 0:
            self.chooseTag=0;
            
            break;
        case 1:
            self.chooseTag=1;
            break;
        case 2:
            self.chooseTag=2;
            break;
        case 3:
            self.chooseTag=3;
            break;
            
        case 4:
            self.chooseTag=4;
            break;
            
        default:
            break;
    }
    
    [self loadTableViewData:YES];
    
}
- (void)EditIntroduceEvent{
    LSEditIntroduceViewController *EIVC = [[LSEditIntroduceViewController alloc] init];
    EIVC.PersonalDataChangedBlock = ^{
        [headView addFasleData];
        if (self.PersonalDataChangedBlock) {
            self.PersonalDataChangedBlock();
        }
    };
    [self presentViewController:EIVC animated:YES completion:nil];
}

-(void)dismissViewController{
//    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
    
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark 🔥 UI
-(void)initTopUI{
    self.chooseTag=0;
    headView=[[LSPersonalHomePageHeadView alloc]initWithFrame:CGRectMake(0,0, LSScreenWidth, 240+LSStatusBarHeight)];
    headView.bottomArray = @[@"发布的话题\n(0)",@"参与的话题\n(0)",@"关注\n(0)",@"粉丝\n(0)",@"黑名单\n(0)"];
    MyLog(@"bottomArray --- %@",headView.bottomArray);
    headView.attentionBu.hidden = YES;
    
    __weak typeof(self) weakSelf = self;
    headView.ReplacePhotoBlock = ^{
        [weakSelf startUpdateUserHeadImageRG];
    };
    
    headView.sortView.butonActionBlock = ^(NSInteger tag) {
        [weakSelf segmentSortButtonEvent:tag];
    };
    
    headView.EditIntroduceBlock = ^{
        [weakSelf EditIntroduceEvent];
    };
    [self.view addSubview:headView];
    
//    UIButton *backItem=[UIButton buttonWithType:UIButtonTypeCustom];
//    backItem.frame=CGRectMake(15,LSStatusBarHeight+10,30, 30);
//    [backItem setImage:[UIImage imageNamed:@"ReturnCustom"] forState:UIControlStateNormal];
//    [backItem addTarget:self action:@selector(dismissViewController) forControlEvents:UIControlEventTouchUpInside];
//    [backItem setImageEdgeInsets:UIEdgeInsetsMake(0, -10, 0, 10)];
//    [headView addSubview:backItem];
    
    [self.view addSubview:self.tableView]; self.tableView.sd_layout.leftEqualToView(self.view).rightEqualToView(self.view).topSpaceToView(headView, 0).bottomEqualToView(self.view);
    
    
}


#pragma mark 🔥 替换头像
-(void)startUpdateUserHeadImageRG{
    
    //    KWeakSelf(self);
    UIActionSheet *sheet  = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍照", @"从相册选择", nil];
    sheet.tag = 898989;
    [sheet showInView:self.view];
    
}
#pragma mark 选中拍照或者相册选择
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    
    
    if (buttonIndex < 2) {
        if (actionSheet.tag == 898989) {
            
            [self actionSheetDidClickedButtonAtIndex:buttonIndex];
            
        }else if (actionSheet.tag == 8989890){
            
            if (buttonIndex == 0) {
//                [self ALAuthorizationStatusButtonIndex:buttonIndex];
                [self openJurisdiction];
            }else{
                return;
            }
            
        }
    }
    else{
        return;
    }
    
}



-(void)actionSheetDidClickedButtonAtIndex:(NSInteger)buttonIndex{
    
    if (buttonIndex==0) {//相机
        AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
        if (authStatus == AVAuthorizationStatusRestricted || authStatus ==AVAuthorizationStatusDenied) {
            // 无权限 引导去开启
            [self ALAuthorizationStatusButtonIndex:buttonIndex];
        }else{
            [self selectImageFormGalleryOrGalleryIndex:buttonIndex];
        }
    }else if (buttonIndex==1){//相册
        
        
        
        //        PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
        
        PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
        if (status == PHAuthorizationStatusRestricted || status == PHAuthorizationStatusDenied){
            // 无权限 做一个友好的提示
            [self ALAuthorizationStatusButtonIndex:buttonIndex];
        }
        
        [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
            if (status == PHAuthorizationStatusAuthorized) {
                MyLog(@"Authorized");
                [self selectImageFormGalleryOrGalleryIndex:buttonIndex];
            }else{
                MyLog(@"Denied or Restricted");
            }
        }];
        
        
    }else{
        return;//取消按钮
    }
    
}


-(void)openJurisdiction{
    NSURL *url=[NSURL URLWithString:UIApplicationOpenSettingsURLString];
    if ([[UIApplication sharedApplication]canOpenURL:url]) {
        [[UIApplication sharedApplication]openURL:url];
    }
}
-(void)ALAuthorizationStatusButtonIndex:(NSInteger)buttonIndex{
    
    //    __block NSInteger tempIndex=buttonIndex;
    
    //    NSString *message=tempIndex==0?@"相机访问受限,需前往授权后才能使用":@"相册访问受限,需前往授权后才能使用";
    //    dispatch_async(dispatch_get_main_queue(), ^{
    UIActionSheet *sheet  = [[UIActionSheet alloc] initWithTitle:@"访问受限" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"前往授权", nil];
    sheet.tag = 8989890;
    [sheet showInView:self.view];
    //    });
    
    
    
}


/*
 #pragma mark - LCActionSheet 代理方法
 - (void)actionSheet:(LCActionSheet *)actionSheet didClickedButtonAtIndex:(NSInteger)buttonIndex {
 if (buttonIndex==2) {
 return;//取消按钮
 }
 [self selectImageFormGalleryOrGalleryIndex:buttonIndex];
 
 }*/


#pragma mark - UIImagePickerControllerDelegate
-(void)selectImageFormGalleryOrGalleryIndex:(NSInteger)selectIndex{
    
    UIImagePickerController *imagePickerController=[[UIImagePickerController alloc]init];
    imagePickerController.delegate=self;
    imagePickerController.allowsEditing=YES;
    if (selectIndex==0) {
        imagePickerController.sourceType=UIImagePickerControllerSourceTypeCamera;
    }else if(selectIndex==1){
        imagePickerController.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
    }
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary] ){
        
        [imagePickerController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : LSNavTitlenColor}];
        imagePickerController.navigationBar.translucent = NO;//导航栏设置为不透明的
        [imagePickerController.navigationBar setTintColor:LSTitleWhiteColor];
        [imagePickerController.navigationBar setBarTintColor:LSNavMainColor];
    }
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
        imagePickerController.allowsEditing=YES;
        if ([UIDevice currentDevice].systemVersion.floatValue >= 8.0) {
            self.modalPresentationStyle = UIModalPresentationOverCurrentContext;
        }
    }
    [self presentViewController:imagePickerController animated:YES completion:nil];
    
}



-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    
    UIImage *image=[self OriginImage:info[UIImagePickerControllerEditedImage] scaleToSize:CGSizeMake(200, 200)];
    NSArray *array=@[image];
    
    MyLog(@"image --- %@",array);
    [picker dismissViewControllerAnimated:YES completion:^{
        
        NSDictionary *parameters=@{@"type":@"0"};
        
        [LSHttpEnclosureTool Upload:LSUSERUPLOADIMG parameters:parameters uploadParamArray:array resBlock:^(id responseObject) {
            if (responseObject) {
                MyLog(@"postImage --- %@",responseObject);
                NSString *urlString=(NSString *)responseObject;
                //                [LSAccountArchive account].UserHeadUrl=@"";
                //                [self.tableView reloadData];
                NSDictionary *headImgDic=@{@"cid":[LSAccountArchive account].Cid,@"headUrl":urlString};
                
                [LSHttpEnclosureTool POST:LSUSERCHANGEHEAD parameters:headImgDic resBlock:^(id responseObject) {
                    if (responseObject) {
                        //http://101.132.148.59:306/userHead/851a5847ca574972b52ab9c85fac6ad3.jpg?t=1528703186
                        MyLog(@"changHead --- %@",responseObject);
                        NSString *userImgString=(NSString *)responseObject;
                        
                        LSAccountCoder *coderModel = [LSAccountArchive account];
                        coderModel.UserHeadUrl=userImgString;
                        [LSAccountArchive saveAccount:coderModel];
                        [self.tableView reloadData];
                        [notifiCenter postNotificationName:@"LOADCIRCLEDATA" object:nil];
                        if (self.PersonalDataChangedBlock) {
                            self.PersonalDataChangedBlock();
                        }
                    }
                }];
            }
        }];
        
    }];
    
}




- (UIImage*)OriginImage:(UIImage *)image scaleToSize:(CGSize)size{
    CGSize newSize;
    if (image.size.height / image.size.width > 1){
        newSize.height = size.height;
        newSize.width = size.height / image.size.height * image.size.width;
    } else if (image.size.height / image.size.width < 1){
        newSize.height = size.width / image.size.width * image.size.height;
        newSize.width = size.width;
    } else {
        newSize = size;
    }
    // 创建一个bitmap的context
    // 并把它设置成为当前正在使用的context
    UIGraphicsBeginImageContextWithOptions(newSize, YES, 0);
    
    //    [[UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0,newSize.width, newSize.height) cornerRadius:newSize.width] addClip];
    
    // 绘制改变大小的图片
    [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
    
    // 从当前context中创建一个改变大小后的图片
    UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    
    // 使当前的context出堆栈
    UIGraphicsEndImageContext();
    
    // 返回新的改变大小后的图片
    return scaledImage;
    
}

@end
